package com.an.vhr.controller.emp.basic;

import com.an.vhr.bean.*;
import com.an.vhr.service.*;
import com.an.vhr.utils.POIUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.xml.crypto.Data;
import java.io.File;
import java.io.IOException;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;

/**
 * @Author: an1089
 **/
@RestController
@RequestMapping("/employee/basic")
public class EmpBasicController {
    @Resource
    EmployeeService employeeService;
    @Resource
    NationService nationService;
    @Resource
    PoliticsstatusService politicsstatusService;
    @Resource
    JobLevelService jobLevelService;
    @Resource
    PositionService positionService;
    @Resource
    DepartmentService departmentService;



    @GetMapping("/")
    public RespPageBean getEmpByPage(@RequestParam(defaultValue = "1") Integer page,
                                     @RequestParam(defaultValue = "10") Integer size,
                                     Employee employee, Date[] beginDateScope){
        return employeeService.getEmpByPage(page, size, employee, beginDateScope);
    }

    @PostMapping("/")
    public RespBean addEmployee(@RequestBody Employee employee){
        if(employeeService.addEmployee(employee) == 1){
            return RespBean.ok("添加成功");
        }
        return RespBean.error("添加失败");
    }

    @GetMapping("/nations")
    public List<Nation> getAllNations() {
        return nationService.getAllNations();
    }

    @GetMapping("/politicsstatus")
    public List<Politicsstatus> getAllPoliticsstatus() {
        return politicsstatusService.getAllPoliticsstatus();
    }

    @GetMapping("/joblevels")
    public List<JobLevel> getAllJobLevels() {
        return jobLevelService.getAllJobLevels();
    }

    @GetMapping("/positions")
    public List<Position> getAllPositions() {
        return positionService.getAllPositions();
    }

    @GetMapping("/getMaxWorkId")
    public RespBean getMaxWorkId(){
        RespBean respBean = RespBean.builder().setStatus(200).setObj(String.format("%08d",employeeService.getMaxWorkId() + 1));
        return respBean;
    }

    @GetMapping("/deps")
    public List<Department> getAllDepartments() {
        return departmentService.getAllDepartmentsByParentId(-1);
    }

    @DeleteMapping("/{id}")
    public RespBean removeEmpById(@PathVariable Integer id){
        if(employeeService.removeEmpById(id) == 1){
            return RespBean.ok("删除成功");
        }
        return RespBean.error("删除失败");
    }

    @PutMapping("/")
    public RespBean updateEmp( @RequestBody Employee employee){
        if(employeeService.updateEmp(employee) == 1){
            return RespBean.ok("更新成功");
        }
        return RespBean.error("更新失败");
    }

    @GetMapping("/export")
    public ResponseEntity<byte[]> exportData(){
        List<Employee> list = ((List<Employee>) employeeService.getEmpByPage(null, null, null,null).getData());
        return POIUtils.employeeToExcel(list);
    }

    @PostMapping("/import")
    public RespBean importData(MultipartFile file) throws IOException {
        List<Employee> list = POIUtils.excelToEmployee(file,nationService.getAllNations(),politicsstatusService.getAllPoliticsstatus(),
                departmentService.getAllDepartmentsWithNoChildren(),positionService.getAllPositions(),jobLevelService.getAllJobLevels());
        if(employeeService.addEmps(list) == list.size()){
            return RespBean.ok("导入成功");
        }
        return RespBean.error("导入失败");
    }

}
